/*
 * @Author: szx
 * @Date: 2021-12-27 15:38:41
 * @LastEditTime: 2021-12-27 15:52:48
 * @Description:
 * @FilePath: \leetcode\200-299\216\216.js
 */
/**
 * @param {number} k
 * @param {number} n
 * @return {number[][]}
 */
var combinationSum3 = function (k, n) {
    const res = [];
    const backtrack = (arr, index, sum) => {
        if (sum > n || arr.length > k) {
            return;
        }
        if (sum == n && arr.length == k) {
            res.push(arr.slice());
            return;
        }
        for (let i = index; i <= 9 - (k - arr.length) + 1; i++) {
            arr.push(i);
            backtrack(arr, i + 1, sum + i);
            arr.pop();
        }
    };
    backtrack([], 1, 0);
    return res;
};
console.log(combinationSum3(2, 18));
